﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Services;
using System.Text;

namespace WebApplication2
{
    public partial class Customer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string connect = @"Server=USER-PC\SQLEXPRESS;Database=Northwind;Trusted_Connection=True";
            string query = "SELECT CustomerID, CompanyName FROM Customers";
            using (SqlConnection conn = new SqlConnection(connect))
            {
                using (SqlCommand cmd = new SqlCommand(query, conn))
                {
                    conn.Open();
                    Customers.DataSource = cmd.ExecuteReader();
                    Customers.DataValueField = "CustomerID";
                    Customers.DataTextField = "CompanyName";
                    Customers.DataBind();
                }
            }
        }
        [WebMethod]
        public static Company FetchCustomer(string CustomerID)
        {
            Company c = new Company();
            string connect = @"Server=USER-PC\SQLEXPRESS;Database=Northwind;Trusted_Connection=True";
            string query = "SELECT CompanyName, Address, City, Region, PostalCode," +
                      "Country, Phone, Fax FROM Customers WHERE CustomerID = @CustomerID";
            if (CustomerID != null && CustomerID.Length == 5)
            {
                using (SqlConnection conn = new SqlConnection(connect))
                {
                    using (SqlCommand cmd = new SqlCommand(query, conn))
                    {
                        cmd.Parameters.AddWithValue("CustomerID", CustomerID);
                        conn.Open();
                        SqlDataReader rdr = cmd.ExecuteReader();
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                c.CompanyName = rdr["CompanyName"].ToString();
                                c.Address = rdr["Address"].ToString();
                                c.City = rdr["City"].ToString();
                                c.Region = rdr["Region"].ToString();
                                c.PostalCode = rdr["PostalCode"].ToString();
                                c.Country = rdr["Country"].ToString();
                                c.Phone = rdr["Phone"].ToString();
                                c.Fax = rdr["Fax"].ToString();
                            }
                        }
                    }
                }
            }
            return c;
        }
    }
}